package com.shiva.oms.logview.api.view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JToolBar;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import com.sf.shiva.oms.common.util.BeanCopyUtil;
import com.shiva.oms.biz.CustomConnectCfgBiz;
import com.shiva.oms.logview.domain.ConnectCfg;

/**
 * 
 * 
 * 
 * <pre>
 * HISTORY
 * ****************************************************************************
 *  ID    DATE            PERSON            REASON
 *  1     2018年4月16日         LA.01367604           Create
 * ****************************************************************************
 * </pre>
 * 
 * @author LA.01367604
 * @version 1.0
 */
public class ConnectCfgGui extends JDialog {

    private final JPanel contentPanel = new JPanel();
    private String[] headerNames = new String[] { "ID", "配置名", "IP地址", "登陆名", "登陆密码", "访问文件路径" };
    private JTable table;
    private DefaultTableModel tableModel;
    private CustomConnectCfgBiz customCfgBiz = CustomConnectCfgBiz.getInstance();

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        try {
            ConnectCfgGui dialog = new ConnectCfgGui();
            dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            dialog.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public ConnectCfgGui() {
        this(null, null);
    }

    public ConnectCfgGui(ConnectCfg connectCfg) {
        this(null, connectCfg);
    }
    /**
     * Create the dialog.
     */
    public ConnectCfgGui(JFrame parent, ConnectCfg connectCfg) {
        super(parent, true);
        setTitle("会话管理");
        setBounds(100, 100, 738, 364);
        getContentPane().setLayout(new BorderLayout());
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        getContentPane().add(contentPanel, BorderLayout.CENTER);
        contentPanel.setLayout(null);
        {
            JToolBar toolBar = new JToolBar();
            toolBar.setBounds(5, 5, 712, 23);
            contentPanel.add(toolBar);
            {
                JButton button = new JButton("新建");
                button.setFont(new Font("微软雅黑", Font.PLAIN, 12));
                button.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                        NewConnectCfgGui dialog = new NewConnectCfgGui(ConnectCfgGui.this);
                        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                        int parentWidth = parent.getWidth();
                        int parentHight = parent.getHeight();
                        dialog.setLocation(parent.getX() + parentWidth / 2 - dialog.getWidth() / 2,
                            parent.getY() + parentHight / 2 - dialog.getHeight() / 2);// 设置窗口居中显示
                        dialog.setVisible(true);
                        fresh();
                    }
                });
                toolBar.add(button);
            }
            {
                JButton button = new JButton("删除");
                button.setFont(new Font("微软雅黑", Font.PLAIN, 12));
                button.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        int selectedRowIndex = table.getSelectedRow();
                        if (selectedRowIndex >= 0) {
                            String id = (String) table.getValueAt(selectedRowIndex, 0);
                            customCfgBiz.removeCfg(id);
                            tableModel.removeRow(selectedRowIndex);
                            table.revalidate();
                        }
                    }
                });
                toolBar.add(button);
            }
            {
                JButton button = new JButton("刷新");
                button.setFont(new Font("微软雅黑", Font.PLAIN, 12));
                button.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                        fresh();
                    }
                });
                toolBar.add(button);
            }
        }
        {
            // 初始化table值
            Object[][] datas = getArrayFromList(customCfgBiz.getCfgList());
//            tableModel = new ConnectCfgTableModel(headerNames, columnTypes, datas);
            tableModel = new DefaultTableModel(datas, headerNames);
            table = new JTable(tableModel);
            table.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
//                    if (e.getClickCount() == 2) {
//                        int selectedRowIndex = table.getSelectedRow();
//                        if (selectedRowIndex > -1) {
//                            String id = (String) table.getValueAt(selectedRowIndex, 0);
//                            ConnectCfg selectedCfg = customCfgBiz.getCfg(id);
//                            if (connectCfg != null) {
//                                BeanCopyUtil.copyProperties(connectCfg, selectedCfg, false);
//                            }
//                            System.out.println(selectedCfg);
//                            setVisible(false);
//                        }
//                    }
                }
            });
            JScrollPane scrollPane = new JScrollPane(table);
            scrollPane.setBounds(5, 28, 712, 260);
            contentPanel.add(scrollPane);
            // scrollPane.setViewportView(table);
        }
        {
            JPanel buttonPane = new JPanel();
            buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
            getContentPane().add(buttonPane, BorderLayout.SOUTH);
            {
                JButton okButton = new JButton("连接");
                okButton.setFont(new Font("微软雅黑", Font.PLAIN, 12));
                okButton.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                        int selectedRowIndex = table.getSelectedRow();
                        if (selectedRowIndex > -1) {
                            String id = (String) table.getValueAt(selectedRowIndex, 0);
                            ConnectCfg selectedCfg = customCfgBiz.getCfg(id);
                            if (connectCfg != null) {
                                BeanCopyUtil.copyProperties(connectCfg, selectedCfg, false);
                            }
                            setVisible(false);
                        } else {
                            System.out.println("cannot find the selected row");
                        }
                    }
                });
                okButton.setActionCommand("OK");
                buttonPane.add(okButton);
                getRootPane().setDefaultButton(okButton);
            }
            {
                JButton cancelButton = new JButton("取消");
                cancelButton.setFont(new Font("微软雅黑", Font.PLAIN, 12));
                cancelButton.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                        setVisible(false);
                    }
                });
                cancelButton.setActionCommand("Cancel");
                buttonPane.add(cancelButton);
            }
        }
        setResizable(false);
    }

    /**
     * 刷新table的显示
     * 
     * @author LA.01367604-2018年4月18日
     */
    private void fresh() {
        List<ConnectCfg> list = customCfgBiz.reloadList();
        System.out.println("len:" + list.size());
        DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
        tableModel.setRowCount(0);// 清除原有行
        tableModel.setDataVector(getArrayFromList(list), headerNames);
        table.revalidate();
    }
    /**
     * list对象集合转数组表格数据
     * 
     * @param list
     * @return
     * @author LA.01367604-2018年4月17日
     */
    private Object[][] getArrayFromList(List<ConnectCfg> list) {
        int maxData = list.size();
        Object[][] data = new Object[maxData][headerNames.length];
        for (int i = 0; i < list.size(); i++) {
            ConnectCfg model = list.get(i);
            data[i][0] = model.getId();
            data[i][1] = model.getCfgName();
            data[i][2] = model.getIp();
            data[i][3] = model.getUser();
            data[i][4] = model.getPassword();
            data[i][5] = model.getFileDir();
        }
        return data;
    }

    /**
     * 数组表格数据转list对象集合
     * 
     * @param data
     * @return
     * @author LA.01367604-2018年4月17日
     */
    private List<ConnectCfg> getListFromArray(Object[][] data) {
        List<ConnectCfg> list = new ArrayList<>();
        for (int i = 0; i < data.length; i++) {
            ConnectCfg model = new ConnectCfg();
            model.setId((String) data[i][0]);
            model.setCfgName((String) data[i][1]);
            model.setIp((String) data[i][2]);
            model.setUser((String) data[i][3]);
            model.setPassword((String) data[i][4]);
            model.setFileDir((String) data[i][5]);
            list.add(model);
        }
        return list;
    }

}
